
namespace ShoppingCart.DataAccess
{
	using System;
	using System.Collections.Generic;
	using System.Data;
	using System.Data.Common;
	using Microsoft.Practices.EnterpriseLibrary.Data;
	using ShoppingCart.Business.Entity;

	/// <summary>
	/// Represents the DataAccess of LineaData.
	/// </summary>
	public class LineaData : Singleton<LineaData>
	{
		#region Attributes

		private readonly Database BaseDatos = DatabaseFactory.CreateDatabase();

		#endregion

		#region Methods

		/// <summary>
		/// Selects all records from the Linea table by a foreign key.
		/// </summary>
		public IList<Linea> Listar()
		{
			var entidades = new List<Linea>();
			var comando = BaseDatos.GetStoredProcCommand("SelectAllLinea");

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					entidades.Add(new Linea
					{
						IdLinea = lector.GetInt32(lector.GetOrdinal("IdLinea")),
						Nombre = lector.GetString(lector.GetOrdinal("Nombre")),
						Descripcion = lector.IsDBNull(lector.GetOrdinal("Descripcion"))? default(string) : lector.GetString(lector.GetOrdinal("Descripcion")),
						Imagen = lector.IsDBNull(lector.GetOrdinal("Imagen"))? default(string) : lector.GetString(lector.GetOrdinal("Imagen"))
					});
				}
			}
			comando.Dispose();
			return entidades;
		}

		/// <summary>
		/// Selects all records from the Linea table by a foreign key.
		/// </summary>
		public Linea Obtener(int idLinea)
		{
			var linea = default(Linea);
			var comando = BaseDatos.GetStoredProcCommand("SelectLinea");

			BaseDatos.AddInParameter(comando, "IdLinea", DbType.Int32, idLinea);

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					linea = new Linea
					{
						IdLinea = lector.GetInt32(lector.GetOrdinal("IdLinea")),
						Nombre = lector.GetString(lector.GetOrdinal("Nombre")),
						Descripcion = lector.IsDBNull(lector.GetOrdinal("Descripcion"))? default(string) : lector.GetString(lector.GetOrdinal("Descripcion")),
						Imagen = lector.IsDBNull(lector.GetOrdinal("Imagen"))? default(string) : lector.GetString(lector.GetOrdinal("Imagen"))
					};
				}
			}
			comando.Dispose();
			return linea;
		}

		/// <summary>
		/// Saves a record to the Linea table.
		/// </summary>
		public int Agregar(Linea linea)
		{
			var comando = BaseDatos.GetStoredProcCommand("InsertLinea");
			BaseDatos.AddOutParameter(comando, "IdLinea", DbType.Int32, 4);
			BaseDatos.AddInParameter(comando, "Nombre", DbType.String, linea.Nombre);
			BaseDatos.AddInParameter(comando, "Descripcion", DbType.String, linea.Descripcion);
			BaseDatos.AddInParameter(comando, "Imagen", DbType.String, linea.Imagen);

			var resultado = BaseDatos.ExecuteNonQuery(comando);
			if (resultado == 0) throw new Exception("Error al Agregar Linea.");

			var valorDevuelto = (int)BaseDatos.GetParameterValue(comando, "IdLinea");;
			comando.Dispose();
			return valorDevuelto;
		}

		/// <summary>
		/// Updates a record in the Linea table.
		/// </summary>
		public bool Modificar(Linea linea)
		{
			var comando = BaseDatos.GetStoredProcCommand("UpdateLinea");
			BaseDatos.AddInParameter(comando, "IdLinea", DbType.Int32, linea.IdLinea);
			BaseDatos.AddInParameter(comando, "Nombre", DbType.String, linea.Nombre);
			BaseDatos.AddInParameter(comando, "Descripcion", DbType.String, linea.Descripcion);
			BaseDatos.AddInParameter(comando, "Imagen", DbType.String, linea.Imagen);

			var resultado = BaseDatos.ExecuteNonQuery(comando);
			if (resultado == 0) throw new Exception("Error al Modificar Linea.");

			comando.Dispose();
			return true;
		}

		/// <summary>
		/// Deletes a record from the Linea table by its primary key.
		/// </summary>
		public bool Eliminar(int idLinea)
		{
			var comando = BaseDatos.GetStoredProcCommand("DeleteLinea");
			BaseDatos.AddInParameter(comando, "IdLinea", DbType.Int32, idLinea);

			var resultado = BaseDatos.ExecuteNonQuery(comando);
			if (resultado == 0) throw new Exception("Error al Eliminar Linea.");
			comando.Dispose();
			return true;
		}

		#endregion
	}
}
